/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.constant.ActivityConstant;
import com.ykht.proj.activity.entity.CnActivity;
import com.ykht.proj.activity.entity.CnActivityShop;
import com.ykht.proj.activity.mapper.CnActivityShopMapper;
import com.ykht.proj.activity.service.CnActivityService;
import com.ykht.proj.activity.service.CnActivityShopService;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.coupon.api.feign.RemoteShopService;
import com.ykht.proj.coupon.vo.AboutShopReq;
import com.ykht.proj.coupon.vo.AboutShopVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商铺活动关联表
 *
 * @author csg
 * @date 2020-12-23 11:13:32
 */
@Service
public class CnActivityShopServiceImpl extends ServiceImpl<CnActivityShopMapper, CnActivityShop> implements CnActivityShopService {

	@Resource
	private RemoteShopService remoteShopService;
	@Resource
	private CnActivityService cnActivityService;

	@Override
	public R<IPage<AboutShopVo>> getCnActivityShopPage(Page<CnActivityShop> page, Long id, String longitude, String latitude) {
		List<CnActivityShop> cnActivityShopList = this.list(Wrappers.lambdaQuery(CnActivityShop.class).eq(CnActivityShop::getActivityId, id));
		List<Long> ids = cnActivityShopList.stream().map(CnActivityShop::getShopId).distinct().collect(Collectors.toList());
		return R.ok(this.AboutShopVoPage(page, ids, longitude, latitude));
	}

	@Override
	public IPage<AboutShopVo> AboutShopVoPage(Page page, List<Long> ids, String longitude, String latitude) {
		AboutShopReq req = new AboutShopReq();
		req.setIds(ids);
		req.setLongitude(longitude);
		req.setLatitude(latitude);
		req.setPage(page);
		Page<AboutShopVo> shopVoPage = remoteShopService.shopByIds(req);
		return shopVoPage;
	}

	@Override
	public R<List<CnActivity>> getCnActivityListBYShopId(Long shopId) {
		//门店关联活动
		List<Long> ids = this.list(Wrappers.lambdaQuery(CnActivityShop.class)
				.eq(CnActivityShop::getShopId, shopId))
				.stream().map(CnActivityShop::getActivityId).distinct().collect(Collectors.toList());
		//活动列表
		if (ids.size()==0)  return R.ok(new ArrayList<>());

		List<CnActivity> list = cnActivityService.list(Wrappers.lambdaQuery(CnActivity.class)
				.in(CnActivity::getId, ids)
				.eq(CnActivity::getDisplay, 0)
				.eq(CnActivity::getExpireStatus, ActivityConstant.ACTIVITY_EXPIRE_0)
				.le(CnActivity::getStartTime, LocalDateTime.now())
				.orderByDesc(CnActivity::getCreateTime));
		return R.ok(list);
	}
}
